package com.thug.states;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

import com.thug.engine.SceneManager;

public class GameHud extends HUD {
	
	final SceneManager mSceneMgr;
	private Sound mBtnSound;

	private Sprite mback_btn_downSprite;
	private Sprite mlevel_uncompletedSprite;
	private Sprite mmenu_black_button_backSprite;
	private Sprite mzoominSprite;
	private Sprite mzoomoutSprite;
	
	private boolean mZoomInPressed;
	private boolean mZoomOutPressed;
	private boolean mBackBtnPressed;
	private boolean mLevelCompletePressed;
	private boolean mLevelNotCompletePressed;
	
	
	public GameHud(SceneManager s) {
		mSceneMgr = s;
		
	}
	
	public void init() {
		//mSceneMgr.getScene().attachChild(this);//(this);
		//mSceneMgr.getScene().re.registerTouchArea(pTouchArea)ouchAreas(this.getTouchAreas());
		
		//this.setCamera(mSceneMgr.getCamera());
		//mSceneMgr.getCamera().setHUD(this);
		mBtnSound = (Sound) mSceneMgr.getResource("menu_btn_sound");
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
		Context context = mSceneMgr.getApplicationContext();
		
		BitmapTextureAtlas mHudTexture= new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		TextureRegion mback_btn_downTexRegion;
		TextureRegion mlevel_uncompletedTexRegion;
		TextureRegion mmenu_black_button_backTexRegion;
		TextureRegion mzoominTexRegion;
		TextureRegion mzoomoutTexRegion;
		
		mback_btn_downTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mHudTexture, context, "back_btn_down.png", 112, 78);
		mlevel_uncompletedTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mHudTexture, context, "level_uncompleted.png", 0, 0);
		mmenu_black_button_backTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mHudTexture, context, "menu_black_button_back.png", 0, 44);
		mzoominTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mHudTexture, context, "zoomin.png", 0, 78);
		mzoomoutTexRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mHudTexture, context, "zoomout.png", 56, 78);
		
		mSceneMgr.getEngine().getTextureManager().loadTexture(mHudTexture);
		
		
		mback_btn_downSprite = new Sprite(0, 0, mback_btn_downTexRegion){
			@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
			if(pSceneTouchEvent.isActionDown()) {
				mBtnSound.play();
				setBackBtnPressed(true);
				return true;
			}
			return super
					.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
		}};
		
		
		mlevel_uncompletedSprite = new Sprite(0, 0, mlevel_uncompletedTexRegion){ 
			@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()) {
					mBtnSound.play();
					setLevelNotCompletePressed(true);
					return true;
				}
			return super
					.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
		}};
		
		
		mmenu_black_button_backSprite = new Sprite(0, 0, mmenu_black_button_backTexRegion){ 
			@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()) {
					mBtnSound.play();
					setLevelCompletePressed(true);
					return true;
				}
			return super
					.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
		}};
		
		
		mzoominSprite = new Sprite(0, 0, mzoominTexRegion){ 
			@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()) {
					mBtnSound.play();
					setZoomInPressed(true);
					return true;
				}
			return super
					.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
		}};
		
		
		mzoomoutSprite = new Sprite(0, 0, mzoomoutTexRegion){
			@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()) {
					mBtnSound.play();
					setZoomOutPressed(true);
					return true;
				}
			return super
					.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
		}};
	}
	
	
	void show(boolean zoomin, boolean zoomout, boolean backbtn, boolean levelcomplete, boolean levelnotcomplete) {
		this.detachChildren();
		this.clearTouchAreas();
		if(zoomin) {
			mzoominSprite.setPosition(mSceneMgr.getScreenWidth() * 2 - mzoominSprite.getWidth(), 0);
			this.attachChild(mzoominSprite);
			this.registerTouchArea(mzoominSprite);
		}
		
		if(zoomout) {
			mzoomoutSprite.setPosition(0, 0);
			this.attachChild(mzoomoutSprite);
			this.registerTouchArea(mzoomoutSprite);
		}
		
		
		if(backbtn) {
			mback_btn_downSprite.setPosition(mzoominSprite.getX() - mback_btn_downSprite.getWidth() - 10, 0);
			this.attachChild(mback_btn_downSprite);
			this.registerTouchArea(mback_btn_downSprite);
		}
		
		if(levelcomplete) {
			mmenu_black_button_backSprite.setPosition(mSceneMgr.getCamera().getCenterX(), mSceneMgr.getCamera().getCenterY());
			this.attachChild(mmenu_black_button_backSprite);
			this.registerTouchArea(mmenu_black_button_backSprite);
		}
		
		if(levelnotcomplete) {
			mlevel_uncompletedSprite.setPosition(mSceneMgr.getCamera().getCenterX(), mSceneMgr.getCamera().getCenterY());
			this.attachChild(mlevel_uncompletedSprite);
			this.registerTouchArea(mlevel_uncompletedSprite);
		}
		
	}

	public boolean isZoomInPressed() {
		return mZoomInPressed;
	}

	public void setZoomInPressed(boolean mZoomInPressed) {
		this.mZoomInPressed = mZoomInPressed;
	}

	public boolean isZoomOutPressed() {
		return mZoomOutPressed;
	}

	public void setZoomOutPressed(boolean mZoomOutPressed) {
		this.mZoomOutPressed = mZoomOutPressed;
	}

	public boolean isBackBtnPressed() {
		return mBackBtnPressed;
	}

	public void setBackBtnPressed(boolean mBackBtnPressed) {
		this.mBackBtnPressed = mBackBtnPressed;
	}

	public boolean isLevelCompletePressed() {
		return mLevelCompletePressed;
	}

	public void setLevelCompletePressed(boolean mLevelCompletePressed) {
		this.mLevelCompletePressed = mLevelCompletePressed;
	}

	public boolean isLevelNotCompletePressed() {
		return mLevelNotCompletePressed;
	}

	public void setLevelNotCompletePressed(boolean mLevelNotCompletePressed) {
		this.mLevelNotCompletePressed = mLevelNotCompletePressed;
	}
	
	public void makeFalse(){
		mZoomInPressed = false;
		mZoomOutPressed = false;
		mLevelCompletePressed = false;
		mLevelNotCompletePressed = false;
		mBackBtnPressed = false;
	}
}
